package cn.glow.readingcloud.account.service.impl;


import cn.glow.readingcloud.account.dao.UserBookshelfMapper;
import cn.glow.readingcloud.account.dto.UserBookshelfDto;
import cn.glow.readingcloud.account.pojo.UserBookshelf;
import cn.glow.readingcloud.account.pojo.UserBookshelfFront;
import cn.glow.readingcloud.account.service.UserBookshelfService;
import cn.glow.readingcloud.account.task.UserBookshelfTask;
import cn.glow.readingcloud.book.feign.client.BookClient;
import cn.glow.readingcloud.common.pojo.Book;
import cn.glow.readingcloud.common.result.Result;
import cn.glow.readingcloud.common.result.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @Author Glow
 * @Date 2023-04-16 15:32:48
 * @Description
 * @Version 1.0
 */
@Service
public class UserBookshelfServiceImpl extends ServiceImpl<UserBookshelfMapper, UserBookshelf> implements UserBookshelfService {
    @Autowired
    private UserBookshelfMapper userBookshelfMapper;
    @Autowired
    private BookClient bookClient;

    @Async("asyncPromiseExecutor")
    @Override
    public Result syncUserBookshelf(Integer userId, UserBookshelfFront userBookshelfFront) {
        UserBookshelf bookshelf = new UserBookshelf();
        BeanUtils.copyProperties(userBookshelfFront, bookshelf);
        bookshelf.setLastReadTime(System.currentTimeMillis());
        // 异步处理同步任务
        UserBookshelfTask task = new UserBookshelfTask(userBookshelfFront.getSyncType(), bookshelf, userBookshelfMapper, userId);
        task.run();;
        return ResultUtil.success();
    }

    @Override
    public Result getUserBookshelf(Integer userId) {
        LambdaQueryWrapper<UserBookshelf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserBookshelf::getUserId,userId);
        List<UserBookshelf> list = userBookshelfMapper.selectList(queryWrapper);
        List<UserBookshelfDto> list1 = new ArrayList<>();
        for(int i = 0 ; i < list.size(); i++)
        {
            UserBookshelf userBookshelf = list.get(i);
            UserBookshelfDto userBookshelfDto = new UserBookshelfDto();
            userBookshelfDto.setUserBookshelf(userBookshelf);
         Book book =  bookClient.getBookById(userBookshelf.getBookId()).getData();
         userBookshelfDto.setAuthorName(book.getAuthorName());
         userBookshelfDto.setBookId(book.getBookId());
         userBookshelfDto.setImgUrl(book.getImgUrl());
         userBookshelfDto.setBookName(book.getBookName());
         list1.add(userBookshelfDto);
        }
        return ResultUtil.success(list1);
    }

    @Override
    public Result userBookshelfExistBook(Integer userId, String bookId) {
        LambdaQueryWrapper<UserBookshelf> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserBookshelf::getUserId,userId);
            queryWrapper.eq(UserBookshelf::getBookId,bookId);

        UserBookshelf userBookshelf = userBookshelfMapper.selectOne(queryWrapper);
        if(userBookshelf == null)
        {
            return ResultUtil.success(0);
        }

        return ResultUtil.success(1);
    }
}
